<template>
  <div id="homePage">
    <!-- 搜索框 -->
    <div class="search-bar">
      <a-input-search
        v-model:value="searchParams.searchText"
        placeholder="请输入关键词搜索"
        enter-button="Search"
        size="large"
        @search="doSearch"
      />
    </div>
    <!-- 标签和分类筛选 -->
    <a-tabs v-model:active-key="selectedCategory" @change="doSearch">
      <a-tab-pane key="all" tab="全部" />
      <a-tab-pane v-for="category in categoryList" :key="category" :tab="category" />
    </a-tabs>
    <div class="tag-bar">
      <span style="margin-right: 8px">标签: </span>
      <a-space :size="[0, 8]" wrap>
        <a-checkable-tag
          v-for="(tag, index) in tagList"
          :key="tag"
          v-model:checked="selectedTag[index]"
          @change="doSearch"
        >
          {{ tag }}
        </a-checkable-tag>
      </a-space>
    </div>
    <!--    <a-list-->
    <!--      :grid="{ gutter: 16, xs: 1, sm: 2, md: 3, lg: 4, xl: 5, xxl: 6 }"-->
    <!--      :data-source="dataList"-->
    <!--      :pagination="pagination"-->
    <!--      :loading="loading"-->
    <!--    >-->
    <!--      <template #renderItem="{ item: picture }">-->
    <!--        <a-list-item style="padding: 0">-->
    <!--          &lt;!&ndash; 单张图片 &ndash;&gt;-->
    <!--          <a-card hoverable @click="doClickPicture(picture)">-->
    <!--            <template #cover>-->
    <!--              <img-->
    <!--                :alt="picture.name"-->
    <!--                :src="picture.thumbnailUrl ?? picture.url"-->
    <!--                style="height: 180px; object-fit: cover"-->
    <!--              />-->
    <!--            </template>-->
    <!--            <a-card-meta :title="picture.name">-->
    <!--              <template #description>-->
    <!--                <a-flex>-->
    <!--                  <a-tag color="green">-->
    <!--                    {{ picture.category ?? '默认' }}-->
    <!--                  </a-tag>-->
    <!--                  <a-tag v-for="tag in picture.tags" :key="tag">{{ tag }}</a-tag>-->
    <!--                </a-flex>-->
    <!--              </template>-->
    <!--            </a-card-meta>-->
    <!--          </a-card>-->
    <!--        </a-list-item>-->
    <!--      </template>-->
    <!--    </a-list>-->
    <PictureList :dataList="dataList" :loading="loading" />
    <a-pagination
      style="text-align: right"
      v-model:current="searchParams.current"
      v-model:page-size="searchParams.pageSize"
      :total="total"
      @change="onPageChange"
    />
  </div>
</template>
<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import {
  listPictureTagCategoryUsingGet,
  listPictureVoByPageUsingPost,
} from '@/api/pictureController'
import { message } from 'ant-design-vue'
import PictureList from "@/components/PictureList.vue";
// 定义数据
const dataList = ref<API.PictureVO[]>([])
// 搜索条件
const searchParams = reactive<API.PictureQueryRequest>({
  current: 1,
  pageSize: 12,
  sortField: 'createTime',
  sortOrder: 'descend',
})
const loading = ref(true)
const total = ref(0)

// 获取分类标签列表
const selectedCategory = ref<string>('all')
const selectedTag = ref<boolean[]>([])

const categoryList = ref<string[]>([])
const tagList = ref<string[]>([])

/**
 * 获取分类标签选项
 */
const getTagCategoryOptions = async () => {
  const res = await listPictureTagCategoryUsingGet()
  // 操作成功
  if (res.data.code === 0 && res.data.data) {
    tagList.value = res.data.data.tagList ?? []
    categoryList.value = res.data.data.categoryList ?? []
  } else {
    message.error('获取分类标签失败. ')
  }
}
onMounted(() => {
  getTagCategoryOptions()
})
const fetchData = async () => {
  loading.value = true

  // 转换标签搜索参数
  const params = {
    ...searchParams,
    tags: [] as string[],
  }
  if (selectedCategory.value !== 'all') {
    params.category = selectedCategory.value
  }
  selectedTag.value.forEach((useTag, index) => {
    if (useTag) {
      params.tags.push(tagList.value[index])
    }
  })
  const res = await listPictureVoByPageUsingPost(params)

  if (res.data.code === 0 && res.data.data) {
    dataList.value = res.data.data.records ?? []
    total.value = res.data.data.total ?? 0
  } else {
    message.error('获取数据失败. ' + res.data.message)
  }
  loading.value = false
}
// 分页参数
const onPageChange = (page: number, pageSize: number) => {
  searchParams.current = page
  searchParams.pageSize = pageSize
  fetchData()
}


const doSearch = () => {
  // 重置搜索条件
  searchParams.current = 1
  fetchData()
}

// 页面加载时加载数据
onMounted(() => {
  fetchData()
})
</script>

<style scoped>
#homePage {
  margin-bottom: 16px;
}

#homePage .search-bar {
  max-width: 480px;
  margin: 0 auto 16px;
}

#homePage .tag-bar {
  margin-bottom: 16px;
}
</style>
